package com.example.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private TextView mQuestionNumView;
    private Button mStartButton;
    private Button mAddButton;
    private static final int REQUEST_CODE_CHEAT = 0;
    private static final String QUESTION = "Question";
    ArrayList<Question> question = new ArrayList<Question>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Qfirstset();

        if(savedInstanceState != null){
            question = (ArrayList<Question>) savedInstanceState.getSerializable(QUESTION);
        }

        mQuestionNumView = (TextView) findViewById(R.id.question_number_view);
        updateNumber();

        mStartButton = (Button) findViewById(R.id.start_button);
        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, QuizActivity.class);
                intent.putExtra("key", question);
                startActivity(intent);
            }
        });

        mAddButton = (Button) findViewById(R.id.add_button);
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, REQUEST_CODE_CHEAT);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHEAT) {
            if (data == null) {
                return;
            }
            Question q = new Question(AddActivity.wasAddShown(data).getTextResId(), AddActivity.wasAddShown(data).isAnswerTrue());
            question.add(q);
        }
        updateNumber();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putSerializable(QUESTION,question);
    }

    private void Qfirstset() {
        Question q1 = new Question("Canberra is the capital of Australia.", true);
        Question q2 = new Question("The Pacific Ocean is larger thanthe Atlantic Ocean.", true);
        Question q3 = new Question("The Suez Canal connects the Red Seaand the Indian Ocean.", false);
        Question q4 = new Question("The source of the Nile River is in Egypt.", false);
        Question q5 = new Question("The Amazon River is the longest riverin the Americas.", true);
        Question q6 = new Question("Lake Baikal is the world\'s oldest and deepestfreshwater lake.", true);
        question.add(q1);
        question.add(q2);
        question.add(q3);
        question.add(q4);
        question.add(q5);
        question.add(q6);

    }

    private void updateNumber() {
        String view = "目前题库中一共有 " + question.size() + " 道题。";
        mQuestionNumView.setText(view);
    }
}

